﻿using Microsoft.AspNetCore.Mvc;
using RBAC_Application.Dto.Input;
using RBAC_Application.Service.RBAC;
using Standard;

namespace B.MES.Login.Controllers
{
    /// <summary>
    /// 用户权限
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private readonly IRbacService _rbacService;

        public AuthController(IRbacService rbacService)
        {
            _rbacService = rbacService;
        }

        /// <summary>
        /// 检查用户权限
        /// </summary>
        [HttpPost("check-permission")]
        public ApiResult<bool> CheckPermission([FromBody] CheckPermissionInput input)
        {
            try
            {
                var hasPermission = _rbacService.CheckUserPermission(input.UserId, input.PermissionName);
                return new ApiResult<bool>
                {
                    Code = ApiCode.Success,
                    ResultData = hasPermission,
                    Message = hasPermission ? "有权限" : "无权限"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<bool>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 获取用户权限列表
        /// </summary>
        [HttpGet("user-permissions/{userId}")]
        public ApiResult<List<string>> GetUserPermissions(long userId)
        {
            try
            {
                var permissions = _rbacService.GetUserPermissionNames(userId);
                return new ApiResult<List<string>>
                {
                    Code = ApiCode.Success,
                    ResultData = permissions,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<string>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }
    }
}